package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Bean.AllyBean;
import activity.glide.GlideApp;

public class FlowPartnerAdapter extends RecyclerView.Adapter<FlowPartnerAdapter.PartnerVH> {

    private final Context mContext;
    private final OnSelectPartnerListener mListener;
    private List<AllyBean.DataBean.ItemsBean> mData;

    public FlowPartnerAdapter(Context context, OnSelectPartnerListener listener) {
        mContext = context;
        mListener = listener;
        mData = new ArrayList<>();
    }

    public void setupData(List<AllyBean.DataBean.ItemsBean> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public PartnerVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new PartnerVH(LayoutInflater.from(mContext).inflate(R.layout.item_select_partner, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull PartnerVH holder, int position) {
        if (position < 0 || position >= mData.size()) {
            return;
        }
        AllyBean.DataBean.ItemsBean bean = mData.get(position);
        GlideApp.with(mContext)
                .load(bean.getBankBackImgPath())
                .error(R.drawable.photo)
                .placeholder(R.drawable.photo)
                .apply(RequestOptions.circleCropTransform())
                .into(holder.avatar);
        holder.name.setText(bean.getRealName());

        if (bean.currTrans != null) {
            switch (bean.currTrans.levelAmount){
                    case 1:
                        holder.level.setBackgroundResource(R.mipmap.r1);
                        break;
                    case 2:
                        holder.level.setBackgroundResource(R.mipmap.r2);
                        break;
                    case 3:
                        holder.level.setBackgroundResource(R.mipmap.r3);
                        break;
                    case 4:
                        holder.level.setBackgroundResource(R.mipmap.r4);
                        break;
                    case 5:
                        holder.level.setBackgroundResource(R.mipmap.r5);
                        break;
                    case 6:
                        holder.level.setBackgroundResource(R.mipmap.r6);
                        break;
                    case 7:
                        holder.level.setBackgroundResource(R.mipmap.r7);
                        break;
                    case 8:
                        holder.level.setBackgroundResource(R.mipmap.r8);
                        break;
                    case 9:
                        holder.level.setBackgroundResource(R.mipmap.r9);
                        break;
                    case 10:
                        holder.level.setBackgroundResource(R.mipmap.r10);
                        break;
                    case 11:
                        holder.level.setBackgroundResource(R.mipmap.r11);
                        break;
                    case 12:
                        holder.level.setBackgroundResource(R.mipmap.r12);
                        break;
                    default:
                        break;
            }

        } else {
            holder.level.setText("R1级");
        }

        holder.itemView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onSelectPartner(bean.getId() + "", bean.getRealName());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class PartnerVH extends RecyclerView.ViewHolder {
        private ImageView avatar;
        private TextView name, level;

        PartnerVH(@NonNull View itemView) {
            super(itemView);
            avatar = itemView.findViewById(R.id.avatar);
            name = itemView.findViewById(R.id.name);
            level = itemView.findViewById(R.id.level);
        }
    }

    public interface OnSelectPartnerListener {
        void onSelectPartner(String id, String partnerName);
    }

}
